package com.zjh.admin.websocket.servive;

import com.alibaba.fastjson.JSONObject;
import com.zjh.admin.websocket.entity.WebsocketDataBody;
import com.zjh.common.entity.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

import static com.zjh.admin.websocket.handle.GlobalWebSocketHandler.sessionMap_id_session;

@Service
public class WebSocketMessageService {

    // 用户登陆通知到所有在线用户
    public void pushLoginMsgToAllOnlineUser(LoginUser loginUser) throws IOException {
        WebsocketDataBody body = new WebsocketDataBody();
        body.setType("loginNotice");
        loginUser.setPasswd("");
        loginUser.setJwt("");
        loginUser.setTokenId("");
        body.setData(loginUser);

        for (WebSocketSession session : sessionMap_id_session.values()) {
            session.sendMessage(new TextMessage(JSONObject.toJSONString(body)));
        }
    }

}
